<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Help;
use Illuminate\Http\Request;

class HelpController extends Controller
{
    public function __construct()
    {
        $this->middleware('admin');
    }

    public function index(Request $request){
        return view('admin/help/index');
    }

    public function getPageRows(Request $request){
        $status = intval($request->input('status'));
        $title = strval($request->input('title'));
        $query = Help::orderBy('id','DESC');
        if($title){
            $query->where('title','like',"%{$title}%");
        }
        if($status){
            $query->where('status',$status);
        }

        $rows =$query->paginate(20);
        foreach($rows as $row){
            $row->content = cutStr2($row->content,70);
        }
        $data =[
            'rows'=>$rows,
        ];
        return $this->response($data);
    }


    public function getCreate(){
        $row = new Help;
        $data ['row'] = $row;
        return view('admin/help/insert',$data);
    }

    public function edit(Request $request){
        $id =  intval($request->input('id'));
        $row = Help::find($id);
        $data ['row'] = $row;
        return view('admin/help/insert',$data);
    }

    public function store(Request $request){
        $title= strval($request->input('title'));
        $content= strval($request->input('content'));
        $id  = intval($request->input('id'));
        if ($id) {
            $row = Help::find($id);
        }else{
            $row = new Help;
        }
        $row->title = $title;
        $row->content =$content;
        $row->Save();
        return $this->response('ok');
    }

    public function postDelete(Request $request){
        $id =  intval($request->input('id'));
        $row = Help::find($id);
        if(!$row){
            return $this->error('记录不存在');
        }
        $row->delete();
        return $this->response('ok');
    }

}
